$(function () {
    init_menu_form();
});

/**初始化表单类型**/
function init_menu_form() {
    //初始化验证数据
    var $menuEditForm = $("#menu-edit-form");
    var icon = "<i class='zmdi zmdi-close-circle zmdi-hc-fw'></i> ";
    $menuEditForm.validate({
        rules: {
            menuName: {
                required: true,
                minlength: 2,
                maxlength: 10,
                remote: {
                    url: "menu/checkMenuName",
                    type: "get",
                    dataType: "json",
                    data: {
                        menuName: function () {
                            return $("input[name='menuName']").val().trim();
                        },
                        type: function () {
                            return $("input[name='type']").val();
                        }
                    }
                }
            }
        },
        messages: {
            menuName: {
                required: icon + "请输入名称",
                minlength: icon + "名称长度2到10个字符",
                remote: icon + "该名称已经存在"
            }
        }
    });
    //初始化菜单栏的数据
    $.post(ctx + "menu/tree", {}, function (r) {
        if (r.code === 0) {
            var data = r.msg;
            $('#menuTree').jstree({
                "core": {
                    'data': data.children,
                    'multiple': false
                },
                "state": {
                    "disabled": true
                },
                "checkbox": {
                    "three_state": false
                },
                "plugins": ["wholerow", "checkbox"]
            });
        } else {
            $JuXue.n_danger(r.msg);
        }
        init_menu_edit_data();
    });
    var $menuName = $menuEditForm.find("input[name='menuName']");
    //初始化类型切换
    $menuEditForm.find("input[name='type']").change(function () {
        var $value = $menuEditForm.find("input[name='type']:checked").val();
        if ($value === "0") {
            $menuName.parent().prev().text("菜单名称：");
        } else {
            $menuName.parent().prev().text("按钮名称：");
        }
    });
}
/**初始化数据**/
function init_menu_edit_data() {
    var selected = $("#menu-data-table").bootstrapTreeTable("getSelections");
    var selected_length = selected.length;
    if (!selected_length) {$JuXue.n_warning('请勾选需要修改的菜单或按钮！');return;}
    if (selected_length > 1) {$JuXue.n_warning('一次只能修改一个菜单或按钮！');return;}

    var menuId = selected[0].id;
    $.post(ctx + "menu/getMenu", {"menuId": menuId}, function (r) {
        if (r.code === 0) {
            var $menuTree = $('#menuTree');
            var menu = r.msg;
            $("input:radio[value='" + menu.type + "']").trigger("click");
            $('#menuName').val(menu.menuName);
            $('#menuId').val(menu.menuId);
           $('#icon').val(menu.icon);
           $("#perms").val(menu.perms);
            $("#url").val(menu.url);
            $menuTree.jstree('select_node', menu.parentId, true);
            $menuTree.jstree('disable_node', menu.menuId);
        } else {
            $JuXue.n_danger(r.msg);
        }
    });
}

/**保存修改的数据**/
function editMenuInfo() {
    var $menueditForm = $("#menu-edit-form");
    var ref = $('#menuTree').jstree(true);
    $("[name='parentId']").val(ref.get_checked()[0]);

    var validator = $menueditForm.validate();
    var flag = validator.form();
    if (flag) {
        $.post(ctx + "menu/update", $menueditForm.serialize(), function (r) {
            if (r.code === 0) {
                $JuXue.n_success(r.msg);
                $("#menu_info").modal("hide");
                Menu_refresh();
            } else $JuXue.n_danger(r.msg);
        });
    }
}
